$(function () {
    $("#jqGrid").Grid({
        url: '../shops/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: 'userId', name: 'userId', index: 'user_id', width: 100, hidden:true},
			{label: '店主姓名', name: 'userName', index: 'user_name',width:100},
			{label: '寝室楼id ', name: 'buildingId', index: 'building_id',hidden:true},
			{label: '寝室楼名称', name: 'buildingName', index: 'building_name', width: 100},
			{label: '学校id ', name: 'schoolId', index: 'school_id',hidden:true},
			{label: '学校名称', name: 'schoolName', index: 'school_name',width:100},
			{label: '商铺名称', name: 'shopName', index: 'shop_name', width: 100},
			{label: '商铺类型', name: 'shopType', index: 'shop_type', width: 100,
				formatter: function (value) {
					return value === 1 ?
						'<span class="label label-danger">普通店铺</span>' :
						'<span class="label label-success">外卖店铺</span>';
				}
			},
			{label: '商铺主图片', name: 'shopPicUrl', index: 'shop_pic_url', width: 100,formatter: function (value) {
				return transImg(value);
				}},
			{label: '商铺地址', name: 'shopAddress', index: 'shop_address', width: 100},
			{label: '商铺公告', name: 'notice', index: 'notice', width: 100},
			{label: '配送费', name: 'shippingFee', index: 'shipping_fee', width: 100},
			{label: '满免配送费', name: 'fullReductionshippingFee', index: 'full_reductionshipping_fee', width: 100},
			{label: '状态', name: 'status', index: 'status', width: 100,formatter: function (value, options, row) {
                return value === 0 ?
                    '<span class="label label-danger">营业中</span>' :
                    '<span class="label label-success">暂停营业</span>';}},
			{label: '排序', name: 'sort', index: 'sort', width: 100},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 100,formatter: function (value) {
                return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }},
			{label: '修改时间', name: 'updateTime', index: 'update_time', width: 100,formatter: function (value) {
                return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
		schoolList: [],
        title: null,
		dorms: {},
		shops: {shopPicUrl:""},
		allDormList:[],
		dormList:[],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
            vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.shops = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		handleSuccess: function (res, file) {
			vm.shops.shopPicUrl = file.response.url;
		},
		handleFormatError: function (file) {
			this.$Notice.warning({
				title: '文件格式不正确',
				desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
			});
		},
		handleMaxSize: function (file) {
			this.$Notice.warning({
				title: '超出文件大小限制',
				desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
			});
		},
		eyeImage: function () {
			var url = vm.shops.shopPicUrl;
			eyeImage(url);
		},
		saveOrUpdate: function (event) {
            let url = vm.shops.id == null ? "../shops/save" : "../shops/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.shops),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../shops/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../shops/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.shops = r.shops;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
	created:function(){
		var that = this;
		//获取所有的学校和宿舍
		Ajax.request({
			type: "POST",
			url: "../regionrelatebedroom/queryAllSchool",
			contentType: "application/json",
			successCallback: function (date) {
				that.schoolList=date.list;
			}
		});
		Ajax.request({
			type: "POST",
			url: "../regionrelatebedroom/queryAllDorm",
			contentType: "application/json",
			successCallback: function (date) {
				that.allDormList=date.list;
			}
		});
	},
	watch:{
		'shops.schoolId': {
			handler() {
				this.dormList = [];
				this.allDormList.forEach((item,index)=>{
					if(item.parentId === this.shops.schoolId){
						this.dormList.push(item);
					}
				})
			},
			deep: true
		}
	}
});